<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      新建银行卡
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="180px">
      <el-form-item label="支付类型：" prop="limitType">
        <el-radio-group v-model="form.limitType">
          <el-radio :label="0">普通存款</el-radio>
          <el-radio :label="1">大额存款</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="银行选择" prop="bankId">
        <el-select v-model="form.bankId">
          <el-option
            v-for="o in bankList"
            :label="o.bankName"
            :value="o.bankId"
            :key="o.bankId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开户支行" prop="bankBranchName" v-if="form.bankId !== zfbId">
        <el-col :span="16">
          <el-input @keyup.enter.native="doSave({action: 'saveForm', form: 'refForm'})" v-model="form.bankBranchName"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="开户人姓名" prop="name">
        <el-col :span="16">
          <el-input @keyup.enter.native="doSave({action: 'saveForm', form: 'refForm'})" v-model="form.name"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="银行卡号" prop="cardNo" v-if="form.bankId !== zfbId">
        <el-col :span="16">
          <el-input @keyup.enter.native="doSave({action: 'saveForm', form: 'refForm'})" v-model="form.cardNo"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="二维码" v-if="form.bankId === zfbId">
        <el-upload
          class="avatar-uploader"
          :action="uploadApi"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.qrcode" :src="form.qrcode" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-tip>*支持gif、jpg、png格式，大小不超过2M</el-tip>
        <el-input  v-model="form.cardNo" type="hidden"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-col :span="16">
          <el-input  type="textarea" v-model="form.remark"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="当日入款上限" prop="dailyLimit">
        <el-col :span="16">
          <el-input-number
            v-model="form.dailyLimit"
            :min="0"
            :controls="false"
            :precision="2"
            :style="{ width: '100%' }"
            class="global-input-number-left-view"
            @keyup.enter.native="doSave({action: 'saveForm', form: 'refForm'})"
          >
          </el-input-number>
        </el-col>
        <el-col :span="24">
          <el-tip>三方支付轮替配置使用，当日此三方是支付收款最大上限</el-tip>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)" :loading="loading">取 消</el-button>
      <el-button type="primary" @click="doSave({action: 'saveForm', form: 'refForm'})" :loading="loading">确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie';
import { addPaymentBank } from '@/services/api/payment';
import { getPayeeBankList } from '@/services/api/recharge';

import { form, factory } from '@/build';

export default {
  name: 'bank-edit-dialog',
  props: {
    rowData: {
      type: Object,
      default: () => {},
    },
    id: {
      type: Number,
      default: undefined,
    },
    value: {
      type: Boolean,
      default: true,
    },
  },
  mixins: [form, factory],
  data() {
    return {
      uploadApi: `${api.UPLOAD_PIC}?token=${Cookies.get(consts.CACHE_TOKEN)}&uid=${Cookies.get(consts.CACHE_UID)}`,
      bankList: [],
      form: {
        id: this.id,
        limitType: 0,
        bankId: undefined,
        bankBranchName: undefined,
        name: undefined,
        cardNo: undefined,
        remark: undefined,
        qrcode: '',
        ...this.rowData,
      },
      applyData: {},
      rules: {
        bankId: [
          { required: true, message: '银行不能为空' },
        ],
        bankBranchName: [
          { required: true, message: '开户支行不能为空' },
        ],
        name: [
          { required: true, message: '开户人姓名不能为空' },
          { pattern: /^[\u4E00-\u9FA5]+$/, message: '姓名只能为中文', trigger: 'blur' },
        ],
        cardNo: factory.getRule('cardNumber'),
        qrcode: [
          { required: true, message: '支付宝二维码不能为空', trigger: 'change' },
        ],
        remark: [
          { required: true, message: '备注为必填项' },
        ],
        dailyLimit: [
          { required: true, message: '当日入款上限为必填项' },
        ],
      },
    };
  },
  computed: {
    zfbId() {
      let bankId = 0;
      _.map(this.bankList, (item) => {
        if (item.bankName === '支付宝二维码') bankId = item.bankId;
      });

      return bankId;
    },
  },
  methods: {
    handleAvatarSuccess(res) {
      this.form.qrcode = res.data;
      this.form.cardNo = res.data;
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isJPG) {
      //   this.$message.error('二维码图片只能是 JPG 格式!');
      // }
      if (!isLt2M) {
        this.$message.error('二维码图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
  },
  mounted() {
    this.bindSave(addPaymentBank, 'saveForm', 'form', {
      beforeSend: data => ({ ...data, id: this.id }),
      afterSuccess: () => {
        this.$emit('input', false);
        this.$parent.gridSearch();
      },
    });

    if (!_.isUndefined(this.rowData)) {
      if (this.rowData.bankId == 101) {
        this.form.qrcode = this.rowData.cardNo;
      }
    }

    this.bindGet(getPayeeBankList, 'bankList', {});
    this.getAll();
  },
};
</script>

<style lang="scss">
  .avatar-uploader {
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }

    .el-upload {
      &:hover {
        border-color: #409EFF;
      }
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
</style>
